package com.ygxsk.carhome.views.picker;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.ygxsk.carhome.R;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-04-13  16:41
 * @Description: 仅仅是月份的选择弹窗；
 */
public class MonthPickerDialogFragment extends DialogFragment {

    //月份的选择器具
    protected MonthPicker mMonthPicker;

    //这里仅仅只有 月份信息
    private int mSelectedMonth = -1;// mSelectedMonth = -1, mSelectedDay = -1;

    //这里选择月份的选择监听器
    private OnMonthChooseListener mOnDateChooseListener;

    private boolean mIsShowAnimation = true;

    protected TextView mCancelButton, mDecideButton;

    public void setOnMonthChooseListener(OnMonthChooseListener onDateChooseListener) {
        mOnDateChooseListener = onDateChooseListener;
    }

    public void showAnimation(boolean show) {
        mIsShowAnimation = show;
    }

    public static MonthPickerDialogFragment newInstance(int number) {

        Bundle args = new Bundle();

        MonthPickerDialogFragment fragment = new MonthPickerDialogFragment();
        // Supply num input as an argument.
        args.putInt("num", number);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialogmonth, container);

        mMonthPicker = (MonthPicker) view.findViewById(R.id.monthpicker_dialog);

        //mYearPicker.setShowCurtainBorder(false);//不要粗体的
        //设置幕布间隔颜色
        //mYearPicker.setCurtainBorderColor(R.color.com_ycuwq_datepicker_divider);

        mCancelButton = (TextView) view.findViewById(R.id.btn_dialog_year_cancel);
        mDecideButton = (TextView) view.findViewById(R.id.btn_dialog_year_decide);
        //2023年5月9日 有个【重置清空】表示不选择月份
        //btn_dialog_month_clear
        TextView mClearButton = (TextView) view.findViewById(R.id.btn_dialog_month_clear);

        if (mClearButton != null) {
            mClearButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //清空重置的按钮；
                    if (mOnDateChooseListener != null) {
                        //这个特殊-设置为-1;什么都没有选中；
                        mOnDateChooseListener.onMonthChoose(-1);
                    }

                    dismiss();
                }
            });
        }

        if (mCancelButton != null) {
            mCancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        if (mDecideButton != null) {
            mDecideButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (mOnDateChooseListener != null) {
                        mOnDateChooseListener.onMonthChoose(mMonthPicker.getSelectedMonth());
                    }

                    dismiss();
                }
            });
        }

        if (mSelectedMonth > 0) {
            setSelectedDate();
        }

        initChild();

        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.DatePickerBottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setContentView(R.layout.dialogmonth);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        Window window = dialog.getWindow();

        if (window != null) {
            if (mIsShowAnimation) {
                window.getAttributes().windowAnimations = R.style.DatePickerDialogAnim;
            }
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.BOTTOM; // 紧贴底部
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
            lp.dimAmount = 0.35f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        return dialog;
    }

    protected void initChild() {

    }

    public void setSelectedDate(int _month) {

        mSelectedMonth = _month;
        //没有月，日
        //mSelectedMonth = month;
        //mSelectedDay = day;

        setSelectedDate();
    }

    private void setSelectedDate() {

        if (mMonthPicker != null) {
            mMonthPicker.setSelectedMonth(mSelectedMonth);
        }

    }

    public interface OnMonthChooseListener {
        void onMonthChoose(int month);
    }

}
